package com.barry.oscheckpoint;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.os.Environment;

public class LoadCheckPoints {
	
	 List<OsCheckPointObject> checkPoints;
	
	public LoadCheckPoints(){
		checkPoints = new ArrayList<OsCheckPointObject>();
		PopulateFromXML("FoxesRun.xml");
	}

	  public List<OsCheckPointObject> getCheckPoints() {
	    return this.checkPoints;
	  }
	  
private void PopulateFromXML(String filename){	
	try {
	    File fXmlFile = new File(Environment.getExternalStorageDirectory().getPath() + "/OSCheckPoint/" + filename);
	    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	    Document doc = dBuilder.parse(fXmlFile);
	    doc.getDocumentElement().normalize();
	    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
	    NodeList nList = doc.getElementsByTagName("c");
	    for (int temp = 0; temp < nList.getLength(); temp++) {
		        OsCheckPointObject obj = new OsCheckPointObject();
		        Node nNode = nList.item(temp);
		        System.out.println("\nCurrent Element :" + nNode.getNodeName());
		        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
		        	org.w3c.dom.Element eElement = (org.w3c.dom.Element) nNode;
		        	obj.setLongt(Double.parseDouble(eElement.getAttribute("o")));
		        	obj.setLat(Double.parseDouble(eElement.getAttribute("a")));
			        obj.setLabel(eElement.getAttribute("l"));
			        obj.setVisited(false);
			        obj.setIndex(temp);
		        }
		        checkPoints.add(obj);
		    }
	    } catch (Exception e) {
	    e.printStackTrace();
	    }
	
}


}
